<?php
namespace App\Http\Controllers\ApiUser;

use App\Service\Member\AddressService;
use Illuminate\Http\Request;

class AddressController extends BaseController
{
    //用户收货地址

    public function __construct(AddressService $service)
    {
        $this->db = $service;
    }

    //列表
    public function getList(Request $request)
    {
        $token = $request->post('token', '');
        $pages['start'] = $request->post('page', 1);
        $pages['limit'] = $request->post('size', 10);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有独登录');
        }
        $res = $this->db->getList($uid, $pages);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data'], $res['total']);
    }

    //详情
    public function getOne(Request $request)
    {
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $uid = $this->db->uidByToken($token);
        $res = $this->db->getOne($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson(0, '', $res['data']);
    }

    //添加
    public function add(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        $pas['accepter'] = $request->post('accepter', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['sex'] = $request->post('sex', 0);
        $pas['door'] = $request->post('door', '');
        $pas['map_name'] = $request->post('map_name', ''); //地图地址
        $pas['map_locate'] = $request->post('map_locate', ''); //地图坐标
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['accepter'] || !$pas['phone'] || !$pas['sex']) {
            return $this->toJson(-2, '收货人、手机号货性别必须的');
        }
        if (!$pas['map_name'] || !$pas['map_locate']) {
            return $this->toJson(-2, '地图地址、门牌号或坐标必须的');
        }
        $res = $this->db->add($pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //修改
    public function modify(Request $request)
    {
        $token = $request->post('token', '');
        $pas['uid'] = $this->db->uidByToken($token);
        $id = $request->post('id', 0);
        $pas['accepter'] = $request->post('accepter', '');
        $pas['phone'] = $request->post('phone', '');
        $pas['sex'] = $request->post('sex', 0);
        $pas['door'] = $request->post('door', '');
        $pas['map_name'] = $request->post('map_name', ''); //地图地址
        $pas['map_locate'] = $request->post('map_locate', ''); //地图坐标
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        if (!$pas['uid']) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$pas['accepter'] || !$pas['phone'] || !$pas['sex']) {
            return $this->toJson(-2, '收货人、手机号货性别必须的');
        }
        if (!$pas['map_name'] || !$pas['map_locate']) {
            return $this->toJson(-2, '地图地址、门牌号或坐标必须的');
        }
        $res = $this->db->modify($id, $pas);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }

    //删除
    public function del(Request $request)
    {
        $token = $request->post('token', '');
        $id = $request->post('id', 0);
        $uid = $this->db->uidByToken($token);
        if (!$uid) {
            return $this->toJson(-2, '没有登录');
        }
        if (!$id) {
            return $this->toJson(-2, 'ID必须的');
        }
        $res = $this->db->del($id);
        if ($res['code'] < 0) {
            return $this->toJson(-2, $res['msg']);
        }
        return $this->toJson();
    }
}